package com.bourse.core.ibatis;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bourse.core.utils.IndiceComposition;
import com.ibatis.sqlmap.client.SqlMapClient;

public class IndiceCompoIbatisDao implements IndiceCompoDao {

	
	private static final Logger LOGGER = Logger.getLogger(IndiceCompoIbatisDao.class);
	
	private SqlMapClient sqlMapClient;

	/**
	 * @see IndiceCompoDao#getIndiceCompo()
	 */
	@SuppressWarnings("unchecked")
	public List<IndiceComposition> getIndiceCompo(){		
		List<IndiceComposition> indiceComposition = new ArrayList<IndiceComposition>();		
		try{
			LOGGER.info("[Indice Compo] - Getting data from database");
			indiceComposition = sqlMapClient.queryForList("GET_INDEX_COMPO");			
		}catch(SQLException e){
			LOGGER.error("[Indice Compo] - Error while getting data from databse "+e.getMessage());			
		}
		return indiceComposition;
	}
	
	
	/**
	 * @see IndiceCompoDao#insertIndiceCompo(List)
	 */
	public void insertIndiceCompo(List<IndiceComposition> indiceCompo){
		
		for(IndiceComposition compo : indiceCompo){
			Map<String, Object> parameters = new HashMap<String, Object>(3);
			parameters.put("isinIndice", compo.getIsinIndice());
			parameters.put("isin", compo.getIsin());
			parameters.put("date", compo.getDate());
			try{
				sqlMapClient.queryForList("INSERT_INDEX_COMPO",parameters);
			}catch(SQLException e){
				LOGGER.error("[Indice Compo] - Error while inserting indiceCompo "+e.getMessage());
			}
		}
		
	}
	
	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}

}
